<?php
define('PAGE_SIZE', 50);
include './include/common.inc.php';
include 'content.class.php';
include './include/workflow.class.php';
$module_name = isset($_GET['module']) ? $_GET['module'] : 'category';
$c = new content($module_name);
$w = new workflow;
$action = isset($action) ? $action : 'manage';
$forward = isset($forward) ? $forward : (HTTP_REFERER ? HTTP_REFERER : $self_url);

$catid = isset($catid) ? $catid : 0;
if($module_name !== 'category')
{
	$cat = $mconf->get_conf($module_name, $catid);
	if(empty($cat))
	{
	    showmessage("该栏目不存在！");
	}
	$siteid = $cat['siteid'];
	$modelid= $cat['modelid'];
	$workflowid = $cat['workflowid'];
}
else
{
	$urlparams = '';
	$cat = isset($CATEGORY[$catid]) ? $CATEGORY[$catid] : '';
	$siteid = $cat['siteid'];
	$modelid= $cat['modelid'];
	$workflowid = $cat['workflowid'];
	if(empty($cat))
	{
	    showmessage("该栏目不存在！");
	}
}
$CURRENT_SITEID = $siteid;
$model = cache_read('model_'.$modelid);
$workflow = $w->process_list($workflowid);
$status_list = cache_read('content_status');
switch($action)
{
    case 'add' : //添加内容

        if(!$c->check_perms($catid, 'CON002'))
        {
            showmessage('您没有权限进行此操作！');
        }
        if(isset($data['title']))
        {
            $data['catid'] = $catid;
            $data['siteid']	= $siteid;
            $data['status'] = STATUS_WAIT;
            $data['username'] = isset($current_user['username']) ? $current_user['username'] : '';
            $data['module'] = $module_name;
            $data['modelid'] = $modelid;
            foreach($model['fields'] as $k=>$v)
            {
                if($v['formtype'] == 'mtypes')
                {
                    if(isset($_POST[$v['field']]))
                    {
                        $s = 0;
                        foreach($_POST[$v['field']] as $_k=>$_v)
                        {
                            if($_k >= $s)
                            {
                                $data[$v['field']] = $_v;
                            }
                        }
                    }
                }
            }
            $id = $c->add($data);
            if(empty($id))
            {
                $c->error || $response->alert("添加失败");
                $response->real_execute();
            }
            if(!in_array($catid, $relcat))
            {
                $relcat[] = $catid;
            }
            $c->add_rel($id, $relcat);
            $response->alert('保存成功');
            $response->redirect($forward);
            $response->execute();
        }
        else
        {
            $select_category = $c->select_category('catid', 'catid', $catid);
            if($module_name == 'category' && $CATEGORY[$catid]['showother'])
            {
                $relcat = $c->get_category_tree(0,'relcat', array(), $modelid);
            }
            $form = $c->create_form($modelid);
            $box = isset($box) ? $box : '';
            include template('admin','content_add');
        }
        break;
    case 'import' : //导入内容

        if(!$c->check_perms($catid, 'CON002'))
        {
            showmessage('您没有权限进行此操作！');
        }
        if(isset($_POST['catid']))
        {
            $pici = $_POST['pici'];
            $data['catid'] = $_POST['catid'];
            $data['siteid']	= $siteid;
            $data['status'] = STATUS_WAIT;
            $data['username'] = isset($current_user['username']) ? $current_user['username'] : '';
            $data['module'] = $module_name;
            $data['modelid'] = $modelid;
            $data_list = getCSVdata($_FILES['data_file']['tmp_name']);
            foreach($data_list as $d)
            {
                if($d['pici']!=$pici)
                {
                    continue;
                }
                foreach($model['fields'] as $k=>$v)
                {
                   
                        if(isset($d[$v['field']]))
                        {
                            if($v['field']=='addtime' && isset($d['addtime']))
                            {
                                $data['addtime'] = is_numeric($d['addtime']) ? $d['addtime'] : strtotime($d['addtime']);
                            }
                            elseif($v['field']=='thumb' && !empty ($d['thumb']))
                            {
                                $data['thumb'] = strtolower(substr($d['thumb'],0,10))=='uploadfile' ? str_replace(substr($d['thumb'],0,10), 'uploadfile', $d['thumb']): 'uploadfile/'.$d['thumb'];
                            }
                            elseif($v['field']=='content')
                            {
                                $data['content'] = iconv('GB2312', 'UTF-8//IGNORE', str_replace('UploadFile', 'uploadfile', $d['content']));
                            }
                            else
                            {
                                $data[$v['field']] = iconv('GB2312', 'UTF-8//IGNORE', $d[$v['field']]);
                            }
                        }
                        $data['addtime'] = isset($data['addtime']) ? $data['addtime'] : time();
                }
                $id = $c->add($data);
                if(empty($id))
                {
                    $c->error || $response->alert("添加失败");
                    $response->real_execute();
                }
                if(!@in_array($catid, $relcat))
                {
                    $relcat[] = $catid;
                }
                $c->add_rel($id, $relcat);
            }
            $response->alert('保存成功');
            $response->redirect($forward);
            $response->execute();
        }
        else
        {
            $select_category = $c->select_category('catid', 'catid', $catid);
            if($module_name == 'category' && $CATEGORY[$catid]['showother'])
            {
                $relcat = $c->get_category_tree(0,'relcat', array(), $modelid);
            }
            $form = $c->create_form($modelid);
            $box = isset($box) ? $box : '';
            include template('admin','content_import');
        }
        break;    
    case 'delete' : //删除内容
        if(!$c->check_perms($catid, 'CON006'))
        {
            showmessage('您没有权限进行此操作！');
        }
        if(!isset($id))
        {
            $response->alert("没有删除任何文章");
            $response->real_execute();
        }
        if($c->status($id, STATUS_RECYCLE))
        {
        	$hook_obj->op_hook('after_del_content', $CURRENT_SITEID, array('contentid'=>$id));
            $response->alert("删除成功");
            $response->script('self.location=self.location');
        }
        $response->execute();
        break;

    case 'delete_all' : //批量删除
        if(!$c->check_perms($catid, 'CON006'))
        {
            $response->alert('您没有权限进行此操作！');
            $response->real_execute();
        }
        if(!isset($selected))
        {
            $response->alert("没有选择任何文章");
            $response->real_execute();
        }
        if(!$c->delete_all($selected, $CURRENT_SITEID))
        {
            $response->alert("删除失败");
            $response->real_execute();
        }
        $response->script('self.location=self.location');
        $response->execute();
        break;

    case 'edit' : //修改内容

        if(!$c->check_perms($catid, 'CON005'))
        {
            showmessage('您没有权限进行此操作！');
        }
        if(empty($id))
        {
            showmessage('该文章不存在');
        }
        if(isset($data['title']))
        {
            $data['catid'] = $catid;
            $data['modelid'] = $modelid;
            foreach($model['fields'] as $k=>$v)
            {
                if($v['formtype'] == 'mtypes')
                {
                    if(isset($_POST[$v['field']]))
                    {
                        $s = 0;
                        foreach($_POST[$v['field']] as $_k=>$_v)
                        {
                            if($_k >= $s)
                            {
                                $data[$v['field']] = $_v;
                            }
                        }
                    }
                }
            }
            if(!$c->edit($id, $data))
            {
                $response->alert($c->error);
                $response->real_execute();
            }
            if(!in_array($catid, $relcat))
            {
                $relcat[] = $catid;
            }
            $c->add_rel($id, $relcat);
            $response->alert('保存成功');
            $response->redirect($forward);
            $response->execute();
        }
        else
        {
            $select_category = $c->select_category('catid', 'catid', $catid);
            $value = $c->get_rels($id);
            if($module_name == 'category' && !empty($CATEGORY[$catid]['showother']))
            {
                $relcat = $c->get_category_tree(0,'relcat', $value, $modelid);
            }
            $data = $c->get_one($id);
            $form = $c->create_form($modelid, $data);
            $s_list = $c->get_audit_status($catid,$workflowid);
            $box = isset($box) ? $box : '';
            include template('admin','content_add');
        }
        break;

    case 'listorder' : //排序
        $c->listorder($info);
        $response->script('self.location=self.location');
        $response->execute();
        break;

    case 'recycle_bin' : //回收站

        if(!$c->check_perms($catid, 'CON004'))
        {
            showmessage('您没有权限进行此操作！');
        }
        if($module_name != 'category')
        {
        	$back = forward_decode($bkforward);
        }
        $condition['catid'] = isset($catid) ? $catid : 0;
        $condition['status'] = STATUS_RECYCLE;
        $page = isset($page) ? intval($page) : 1;
        $data = $c->listinfo($condition, $page, PAGE_SIZE);
        $pages = $c->pages;
        include template('admin', 'content_recycle_bin');
        break;

    case 'delete_select' : //彻底删除选中的文章

        if(!$c->check_perms($catid, 'CON004'))
        {
            $response->alert('您没有权限进行此操作！');
            $response->real_execute();
        }
        if(!isset($selected))
        {
            $response->alert("没有选择任何文章");
            $response->real_execute();
        }
        if(!$c->delete($selected))
        {
            $response->alert("删除失败");
            $response->real_execute();
        }
        $response->script('self.location=self.location');
        $response->execute();
        break;

    case 'clear_all' : //清空回收站

        if(!$c->check_perms($catid, 'CON004'))
        {
            $response->alert('您没有权限进行此操作！');
            $response->real_execute();
        }
        if(!$c->clear_all($catid))
        {
            $response->alert("删除失败");
            $response->real_execute();
        }
        $response->script('self.location=self.location');
        $response->execute();
        break;

    case 'recover_select' : //还原选中的文章

        if(!$c->check_perms($catid, 'CON004'))
        {
            $response->alert('您没有权限进行此操作！');
            $response->real_execute();
        }
        if(!isset($selected))
        {
            $response->alert("没有选择任何文章");
            $response->real_execute();
        }
        if(!$c->recover($selected))
        {
            $response->alert("还原失败");
            $response->real_execute();
        }
        $response->script('self.location=self.location');
        $response->execute();
        break;

    case 'recover_all' : //全部还原

        if(!$c->check_perms($catid, 'CON004'))
        {
            $response->alert('您没有权限进行此操作！');
            $response->real_execute();
        }
        if(!$c->recover_all($catid,$module_name))
        {
            $response->alert("还原失败");
            $response->real_execute();
        }
        $response->script('self.location=self.location');
        $response->execute();
        break;

    case 'audit' : //审核列表
		if($module_name != 'category')
        {
        	$back = forward_decode($bkforward);
        }
        $processid = isset($processid) ? $processid : 0;
        if(!$c->check_perms($catid, 'process_'.$processid))
        {
            showmessage('您没有权限进行此操作！');
        }
        $condition['catid'] = isset($catid) ? $catid : 0;
        $prestatus = $workflow[$processid]['prestatus'];
        $status = isset($status) ? $status : '';
        $condition['status'] = $status ? $status : $prestatus;

        $prestatus = explode(',', $prestatus);
        $page = isset($page) ? intval($page) : 1;
        $data = $c->listinfo($condition, $page, PAGE_SIZE);
        $pages = $c->pages;
        include template('admin', 'content_audit');
        break;

    case 'status' : //更改文章审核状态

        $val = intval($val);
        if(is_array($selected))
        {
            foreach($selected as $s)
            {
                $c->status($s, $val);
            }
            $response->alert("审核成功！");
            $response->script('self.location=self.location');
        }
        else
        {
            $response->alert("请选择内容进行审核！");
        }
        $response->execute();
        break;

    default : //列表及查询

        if(!$c->check_perms($catid, 'CON001'))
        {
            showmessage('您没有权限进行此操作！');
        }
        if(!check_perms('can_view_other_news'))
        {
            $condition['author'] = $current_user['username'];
        }
        
        unset($status_list[0]);//删除的文章不出现在文章列表中
        $condition['catid'] = isset($catid) ? $catid : 0;
        $condition['status'] = isset($status) ? $status : '';
        $condition['type'] = isset($type) ? intval($type) : '';
        $condition['searchtext'] = isset($searchtext) ? $searchtext : '';
        $condition['starttime'] = isset($starttime) ? $starttime : '';
        $condition['endtime'] = isset($endtime) ? $endtime : '';
        $page = isset($page) ? intval($page) : 1;
        
        $data = $c->listinfo($condition, $page, PAGE_SIZE);
        $pages = $c->pages;
        
        if($module_name != 'category')
        {
        	$back = forward_decode($bkforward);
        }
        include template('admin', 'content_manage');
        break;
}
?>
